package com.yonyou.crm.sys.modules.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.sys.modules.entity.FieldTypeVO;
import com.yonyou.crm.sys.modules.entity.FieldVO;
import com.yonyou.crm.sys.modules.entity.ModuleVO;

@Service
class FieldHandler extends BaseBizServiceImpl<FieldTypeVO>{
	
	@Autowired
	private ModuleBizServiceImpl moduleBizServiceImpl;
	@Autowired
	private FieldBizServiceImpl fieldBizServiceImpl;
	/**
	 * 设置可用槽位
	 * @return
	 */
	public void setSlotAndApiName(FieldVO fieldVO) {
		List<FieldTypeVO> fieldTypeVOs = getFieldTypeList();
		String mysqlDatatype = getMysqlDataType(fieldVO.getType(), fieldTypeVOs);
        ModuleVO moduleVO = moduleBizServiceImpl.getModule(fieldVO.getObjId());
        Integer totalFieldNum = moduleBizServiceImpl.getTotalNumForFieldType(moduleVO,mysqlDatatype);
        List<FieldVO> customFields = fieldBizServiceImpl.getCustomFieldList(moduleVO.getId(),mysqlDatatype);
        setNextSlotAndApiName(fieldVO,mysqlDatatype,totalFieldNum,customFields) ;
	}
	
	public List<FieldTypeVO> getFieldTypeList() {
		return dao.selectVoList(FieldTypeVO.class, "ListFieldType", null);
	}
	
	private String getMysqlDataType(Integer type,List<FieldTypeVO> fieldTypeVOs) {
		String mysqlDatatype = "";
		for (FieldTypeVO fieldTypeVO: fieldTypeVOs) {
			if (fieldTypeVO.getType() == type) {
				mysqlDatatype = fieldTypeVO.getMysqlDatatype();
			}
		}
		return mysqlDatatype;
	}
	
	private Boolean hasEmptySlot(Integer totalFieldNum,List<FieldVO> customFields) {
		Boolean rs = false;
		if (totalFieldNum > customFields.size() ) {
			rs = true;
		}
		return rs;
	}
	
	private void setNextSlotAndApiName(FieldVO fieldVO,String mysqlDatatype,Integer totalFieldNum,List<FieldVO> customFields) {
		if (!hasEmptySlot(totalFieldNum, customFields)) {
			throw new CrmBusinessException("该类型自定义字段已使用完");
		}
		Integer slotNum = 0;
		slotNum = getSlotNum(mysqlDatatype, totalFieldNum , customFields)	;
		fieldVO.setSlot( mysqlDatatype+slotNum.toString());
		fieldVO.setApiName( mysqlDatatype+slotNum.toString());
	}
	
	private Integer getSlotNum (String mysqlDatatype, Integer totalFieldNum,List<FieldVO> customFields) {
		Integer slotNum = 0;
		Integer[] slotNums = new Integer[customFields.size()];
		for(Integer i = 0; i<slotNums.length; i++ ) {
			FieldVO customField= customFields.get(i);
			String customFieldSlot = customField.getSlot();
			Integer customFieldSlotNum = Integer.parseInt(customFieldSlot.substring(mysqlDatatype.length()));
			slotNums[i] = customFieldSlotNum;
		}
		Arrays.sort(slotNums);
		Integer totalCustomFieldNum = slotNums.length;
		for (int j = 0; j<totalFieldNum;j++) {
			if (j == totalCustomFieldNum) {
				slotNum = j;
				break;
			}
			if (j < slotNums[j]) {
				slotNum = j;
				break;
			}
		}
		return slotNum;
	}
	
}



